<?php

namespace app\adminapi\controller\v1\exam;

use app\adminapi\controller\AuthController;
use app\exceptions\AdminException;
use app\exceptions\AuthException;
use app\services\exam\ExamPaperService;
use think\facade\App;

class ExamPaper extends AuthController
{
    public function __construct(App $app, ExamPaperService $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    public function index()
    {
        $where = $this->request->getMore([
            ['website_id', $this->website],
            ['page', 1],
            ['pageSize', 20],
            ['title', ''],
            ['pattern', ''],
        ]);
        return app()->make('json')->successful($this->services->index($where));
    }

    public function creatOrUpdate()
    {
        $where = $this->request->getMore([
            ['id', ''],
            ['title', ''],
            ['pattern', ''],
            ['score', ''],
            ['topic_num', ''],
            ['questions', ''],
            ['page', 1],
            ['page_size', 10],
            ['website_id', $this->website],
        ]);
        if (!isset($where['pattern'])) throw new AdminException('缺少必要参数');
        if (!$where['title']) unset($where['title']);
        if (!$where['score']) unset($where['score']);
        if (!$where['topic_num']) unset($where['topic_num']);
        if (!$where['questions']) unset($where['questions']);
        if (!$where['pattern']) unset($where['pattern']);
        return app()->make('json')->successful($this->services->creatOrUpdate($where));
    }

    public function delete()
    {
        $where = $this->request->getMore([
            ['id', ''],
        ]);
        if (!$where['id']) throw new AuthException('缺少必要参数');
        return app()->make('json')->successful($this->services->delete($where));
    }

    public function searchPaperTopic(){

        $where = $this->request->getMore([
            ['id', ''],
        ]);
        if (!$where['id']) throw new AuthException('缺少必要参数');
        return app()->make('json')->successful($this->services->searchPaperTopic($where));
    }





}